package inferenceengine;

import java.util.ArrayList;
import java.util.List;

import commondata.ITeam;
import commondata.ITradeAdviser;
import commondata.NbaPool;
import commondata.TradeAttributesEnum;
import commondata.TradeGoalEnum;

public class TradeAdviser implements ITradeAdviser {

	private static final int BEST_SIXTEEN_TEAM = 15;
	private static final int BEST_FIVE_TEAM = 24;
	private static final int TEAM_NUMBER = 30;
	double pointPG = 0;
	double reboundPG = 0;
	double blockPG = 0;
	double stealPG = 0;
	double assistPG = 0;

	private NbaPool instance;

	public TradeAdviser() {
		instance = NbaPool.getInstance();
	}

	public List<TradeAttributesEnum> createTradeSearchAttributes(ITeam team,
			TradeGoalEnum goalEnum) {

		List<TradeAttributesEnum> tradeattributes = new ArrayList<TradeAttributesEnum>();
		generateAverage(goalEnum);

		if (team.getPointPG() < pointPG) {
			tradeattributes.add(TradeAttributesEnum.Point);
		}
		if (team.getReboundPG() < reboundPG) {
			tradeattributes.add(TradeAttributesEnum.Rebound);
		}
		if (team.getBlockPG() < blockPG) {
			tradeattributes.add(TradeAttributesEnum.Block);
		}
		if (team.getStealPG() < stealPG) {
			tradeattributes.add(TradeAttributesEnum.Steal);
		}
		if (team.getAssistPG() < assistPG) {
			tradeattributes.add(TradeAttributesEnum.Assist);
		}
		return tradeattributes;
	}

	public List<String> getAdvise(ITeam team, TradeGoalEnum goalEnum) {
		List<String> advices = new ArrayList<String>();
		generateAverage(goalEnum);
		if (team.getPointPG() < pointPG) {
			advices.add("Try to increase points per game from "
					+ formatDouble(team.getPointPG()) + " to " + pointPG);
		}
		if (team.getReboundPG() < reboundPG) {
			advices.add("Try to increase rebounds per game from "
					+ formatDouble(team.getReboundPG()) + " to " + reboundPG);
		}
		if (team.getBlockPG() < blockPG) {
			advices.add("Try to increase blocks per game from "
					+ formatDouble(team.getBlockPG()) + " to " + blockPG);
		}
		if (team.getStealPG() < stealPG) {
			advices.add("Try to increase steals per game from "
					+ formatDouble(team.getStealPG()) + " to " + stealPG);
		}
		if (team.getAssistPG() < assistPG) {
			advices.add("Try to increase assists per game from "
					+ formatDouble(team.getAssistPG()) + " to " + assistPG);
		}
		return advices;
	}

	private void generateAverage(TradeGoalEnum ogoalEnumalEnum) {

		if (ogoalEnumalEnum.equals(TradeGoalEnum.TAKE_THE_RING)) {
			generateAverage(BEST_FIVE_TEAM);
		} else {
			generateAverage(BEST_SIXTEEN_TEAM);
		}

	}

	private void generateAverage(int index) {

		List<Double> pointsList = instance.getPointsPG();
		List<Double> reboundsList = instance.getReboundsPG();
		List<Double> stealsList = instance.getStealsPG();
		List<Double> blocksList = instance.getBlocksPG();
		List<Double> assistList = instance.getAssistPG();
		pointPG = 0;
		reboundPG = 0;
		blockPG = 0;
		stealPG = 0;
		assistPG = 0;
		int iterCount = 0;
		for (; index < TEAM_NUMBER; index++) {
			pointPG += pointsList.get(index);
			reboundPG += reboundsList.get(index);
			blockPG += blocksList.get(index);
			stealPG += stealsList.get(index);
			assistPG += assistList.get(index);
			iterCount++;
		}
		pointPG = formatDouble(pointPG /iterCount);
		reboundPG = formatDouble(reboundPG /iterCount);
		blockPG = formatDouble(blockPG /iterCount);
		stealPG = formatDouble(stealPG /iterCount);
		assistPG = formatDouble(assistPG /iterCount);

	}
	
	double formatDouble(double d){
		int in = (int) (d * 1000);
		return ((double)in / 1000);
	}

}
